package com.highspeed.bdk.controller.command;


import com.bianmaba.spring.data.jpa.domain.PageableRequest;
import com.bianmaba.spring.data.jpa.domain.PageableResult;
import com.bianmaba.spring.data.jpa.domain.Result;
import com.bianmaba.spring.mvc.method.support.annotations.JSON;
import com.highspeed.bdk.bean.entity.ViewPostion;
import com.highspeed.bdk.bean.entity.deploy.DeployTopology;
import com.highspeed.bdk.controller.base.BdkCrudController;
import com.highspeed.bdk.service.base.IBdkBaseService;
import com.highspeed.bdk.service.deploy.IDeployTopologyService;
import com.highspeed.bdk.service.physical.IViewPositionService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping(value = "view_position", method = RequestMethod.POST)
@Api(tags = "位置信息接口")
public class ViewPositionController extends BdkCrudController<ViewPostion, Integer> {
    @Autowired
    private IViewPositionService viewPositionService;

    @Override
    protected IViewPositionService getService() {
        return viewPositionService;
    }

    @ApiOperation(value = "保存设备或应用位置信息", notes = "参数为JSON数据;")
    @JSON
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @Override
    public Result save(@RequestBody @ApiParam(name = "entity") ViewPostion entity) {
        return super.save(entity);
    }

    @ApiOperation(value = "批量保存保存设备或应用位置信息,", notes = "参数为JSON数据;")
    @JSON
    @RequestMapping(value = "/clear_saves", method = RequestMethod.POST)
    public Result clearSaves(@RequestBody @ApiParam(name = "data") ClearSavesRequestBody data) {
        return viewPositionService.save(data.getTopologyId(), data.getTopologyTypeName(), data.getViewPostions());
    }

    @JSON
    @ApiOperation(
            value = "保存数据集合",
            notes = "返回数据中的success为true表示操作成功"
    )
    @RequestMapping(
            value = {"/saves"},
            method = {RequestMethod.POST}
    )
    public Result save(List<ViewPostion> entities) {
        return super.save(entities);
    }

    @JSON
    @ApiOperation(
            value = "根据拓扑图ID查询该拓扑图的所有位置信息"
    )
    @RequestMapping(
            value = {"/query_topology"},
            method = {RequestMethod.POST}
    )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "topologyId", value = "拓扑图ID", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "topologyType", value = "拓扑图类型", dataType = "string", paramType = "query")}
    )
    public PageableResult query(String topologyId, String topologyType) {
        Assert.state(topologyId != null, "参数“topologyId”为必填参数，不能为空！");
        Assert.state(topologyType != null, "参数“topologyType”为必填参数，不能为空！");
        LinkedHashMap<String, Object> params = new LinkedHashMap<>(0);
        params.put("topologyId_eq", topologyId);
        params.put("topologyTypeName_eq", topologyType);
        return super.query(params, null);
    }

    @JSON
    @ApiOperation(
            value = "根据拓扑图ID及设备类型名称查询该拓扑图下所有指定的设备类型的位置信息"
    )
    @RequestMapping(
            value = {"/query_topology_and_device_type"},
            method = {RequestMethod.POST}
    )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "topologyId", value = "拓扑图ID", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "topologyType", value = "拓扑图类型", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "deviceType", value = "设备类型名称", dataType = "string", paramType = "query")}
    )
    public PageableResult query(String topologyId, String topologyType, String deviceType) {
        Assert.state(topologyId != null, "参数“topologyId”为必填参数，不能为空！");
        Assert.state(topologyType != null, "参数“topologyType”为必填参数，不能为空！");
        LinkedHashMap<String, Object> params = new LinkedHashMap<>(0);
        params.put("topologyId_eq", topologyId);
        params.put("topologyTypeName_eq", topologyType);
        if (deviceType != null && !deviceType.trim().isEmpty()) {
            params.put("deviceTypeName_eq", deviceType);
        }
        return super.query(params, null);
    }

    public static class ClearSavesRequestBody {
        private Integer topologyId;
        private String topologyTypeName;
        private List<ViewPostion> viewPostions = new ArrayList<>(0);

        public Integer getTopologyId() {
            return topologyId;
        }

        public void setTopologyId(Integer topologyId) {
            this.topologyId = topologyId;
        }

        public String getTopologyTypeName() {
            return topologyTypeName;
        }

        public void setTopologyTypeName(String topologyTypeName) {
            this.topologyTypeName = topologyTypeName;
        }

        public List<ViewPostion> getViewPostions() {
            return viewPostions;
        }

        public void setViewPostions(List<ViewPostion> viewPostions) {
            this.viewPostions = viewPostions;
        }
    }
}
